﻿using DataScripter.Database;
using DataScripter.Scripting;
using DataScripter.Scripting.Query.SqlServer;
using DataScripter.Scripting.ScriptDefinitions;
using DataScripter.UnitTests.Factories;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DataScripter.UnitTests.DataProducerTests
{
    [TestClass]
    public class WhenTwoForeignKeyFilterHasBeenAppliedToTwoNonFilteredTables
    {
        private readonly string _query;

        public WhenTwoForeignKeyFilterHasBeenAppliedToTwoNonFilteredTables()
        {
            const string referencedTableName = "referencedDef";
            const string referencedTableName2 = "referencedGhi";
            const string myTableName = "abc";

            Mock<Db> dbMock = new Mock<Db>("", "");
            Engine engine = new Engine();
            IScriptDefinition scriptDefinition = new ScriptDefinition("", engine);
            scriptDefinition.InstallScriptRequest(referencedTableName);
            scriptDefinition.InstallScriptRequest(referencedTableName2);

            IForeignKey myForeignKey = new ForeignKeyInfo("myFK", "dbo", myTableName, "myColumnName", "dbo", referencedTableName, "theirColumnName");
            IForeignKey myForeignKey2 = new ForeignKeyInfo("myFK2", "dbo", myTableName, "myColumnName2", "dbo", referencedTableName2, "theirColumnName2");
            ITableScriptRequest tableScriptRequest = scriptDefinition.InstallScriptRequest(myTableName, new []{myForeignKey, myForeignKey2});

            DataProducer dataProducer = new DataProducer(dbMock.Object);

            _query = dataProducer.ProduceQuery(tableScriptRequest);
        }

        [TestMethod]
        public void QueryShouldBeCorrect()
        {
            Assert.AreEqual("SELECT * FROM abc WHERE myColumnName IN (SELECT theirColumnName FROM referencedDef) AND myColumnName2 IN (SELECT theirColumnName2 FROM referencedGhi)", _query);
        }
    }
}